﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.MemberInfo
{
    public partial class MemberInfoManage_E_Particular : PageBaseAdmin
    {
        #region 只读资料定义
        private readonly D_T_Member_Exp _Dal = new D_T_Member_Exp();

        #endregion

        #region 固定方法
        /// <summary>
        ///  设置按钮资料
        /// </summary>
        /// <param name="i"></param>
        /// <param name="but"></param>
        private void SetBut(int i, Button but)
        {
            AdminPublic.Try(() =>
            {
                switch(i)
                {
                    case 1:
                        but.CausesValidation = false;
                        break;
                    default:
                        break;
                }
            });

        }
        /// <summary>
        /// 初始化窗口控件
        /// </summary>
        private void ControlsInit()
        {
            AdminPublic.Try(() =>
            {
                DropDownList2.Items.Clear();
                DropDownList2.Items.Add(new ListItem("正常", ((int)M_T_Member_Exp.Freeze_Type.normal).ToString()));
                DropDownList2.Items.Add(new ListItem("冻结", ((int)M_T_Member_Exp.Freeze_Type.freeze).ToString()));
                DropDownList2.Items.Add(new ListItem("永远冻结", ((int)M_T_Member_Exp.Freeze_Type.foreverfreeze).ToString()));
                DropDownList3.Items.Clear();
                DropDownList3.Items.Add(new ListItem("正常", ((int)M_T_Member_Exp.Freeze_Type.normal).ToString()));
                DropDownList3.Items.Add(new ListItem("冻结", ((int)M_T_Member_Exp.Freeze_Type.freeze).ToString()));
                DropDownList3.Items.Add(new ListItem("永远冻结", ((int)M_T_Member_Exp.Freeze_Type.foreverfreeze).ToString()));

            });
        }
        /// <summary>
        /// 编辑模式的时候,填充窗口
        /// </summary>
        /// <param name="model"></param>
        private void SetControlsData(M_T_Member model)
        {
            AdminPublic.Try(() =>
            {
                txtAQD1.Text = model.AnswerA;
                txtAQD2.Text = model.AnswerB;
                txtAQW1.Text = model.ProblemA;
                txtAQW2.Text = model.ProblemB;
                txtDH.Text = model.Tel;
                txtSFZ.Text = model.IDCard;
                txtSR.Text = model.Birthday.Exp_GetString();
                txtXB.Text = model.Sex ? "男" : "女";
                txtXM.Text = model.Name;
                txtYJ.Text = model.Email;
                txtJB.Text = model.Gold.Exp_Money();
                txtJF.Text = model.Grade.ToString();
                txtBGL.Text = model.WinStock.ToString() + "%";
                txtYZ.Text = model.isApprove ? "已验证" : "未验证";

                this.Calendar1.SelectedDate = model.FreezeDate;
                this.Calendar2.SelectedDate = model.OpFreezeDate;

                this.DropDownList2.SelectedValue = model.FreezeType.ToString();
                this.DropDownList3.SelectedValue = model.OpFreezeType.ToString();


            });
        }
        /// <summary>
        /// 从页面控件中构造一个实体model
        /// </summary>
        /// <returns></returns>
        private M_T_Member ControlsToModel()
        {
            var model = new M_T_Member()
            {
                id = ViewState["ID"].Exp_IntTryParse(),
                OpFreezeType = DropDownList3.SelectedValue.Exp_IntTryParse(),
                OpFreezeDate = this.Calendar2.SelectedDate,
                FreezeType = DropDownList2.SelectedValue.Exp_IntTryParse(),
                FreezeDate = this.Calendar1.SelectedDate,
                isApprove = txtYZ.Text == "已验证"
            };
            return model;
        }
        /// <summary>
        /// 操作完成后,处理各种提示用
        /// </summary>
        /// <param name="iRef"></param>
        /// <param name="LogType"></param>
        public void EditSaveMsg(int iRef, Log_Type LogType)
        {
            if(iRef > 0)
            {
                this.Exp_PageWriteLog(LogType, "会员详细", iRef);
                this.Exp_PageSkyAlterReplace("MemberInfoManage.aspx", WebUserToolBar1.EditSKey, "操作成功");
            }
            else
            {
                switch(iRef)
                {
                    //case 0:
                    //    this.Exp_PageAlter("操作失败,操作中出现异常");
                    //    break;
                    //case -1:
                    //    this.Exp_PageAlter("操作失败,会员资料操作失败");
                    //    break;
                    //case -2:
                    //    this.Exp_PageAlter("操作失败,权限组资料操作失败");
                    //    break;
                    default:
                        this.Exp_PageAlter("操作失败");
                        break;
                }
            }
        }
        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {


            AdminPublic.Try(() =>
            {

                if(!IsPostBack)
                {
                    //初始化权限组下拉框
                    ControlsInit();
                    //存储主键
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("ID", id);
                    if(id > 0)
                    {
                        var model = _Dal.GetModel(id);
                        if(model == null)
                        {
                            this.Exp_PageSkyAlterReplace("MemberInfoManage.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            SetControlsData(model);
                        }
                    }
                }
                //工具栏初始化
                WebUserToolBar1.EditInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr);
                WebUserToolBar1.ButtonBegin("冻结保存", "密码重置", "验证状态转换", "返回");
            });

        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                GetModelKey(model =>
                {
                    var refint = _Dal.UpdateFreeze(
                        model.id,
                        model.FreezeType,
                        model.FreezeDate,
                        model.OpFreezeType,
                        model.OpFreezeDate);
                    if(refint > 0)
                    {
                        this.Exp_PageWriteLog(Log_Type.other, "会员管理", refint, "冻结状态");
                        this.Exp_PageAlter("冻结状态维护成功");
                        SetControlsData(_Dal.GetModelFlag(model.id));
                    }
                    else
                    {
                        this.Exp_PageAlter("冻结状态维护失败");
                    }

                });
            });

        }
        /// <summary>
        /// 根据id获取实体,然后执行相关操作
        /// </summary>
        /// <param name="func"></param>
        private void GetModelKey(Action<M_T_Member> func)
        {
            var model = ControlsToModel();
            if(0 >= model.id)
            {
                this.Exp_PageAlter("资料不存在,可能被其他人删除");
                return;
            }
            if(!_Dal.ExistsFlag(model.id))
            {
                this.Exp_PageAlter("资料不存在,可能被其他人删除!");
                return;
            }
            func(model);

        }
        /// <summary>
        /// 密码重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                GetModelKey(model =>
                {
                    var RP = _Dal.ResetPassWord(model.id, "abc");
                    if(RP > 0)
                    {
                        this.Exp_PageWriteLog(Log_Type.other, "会员管理", model.id, "密码重置");
                        this.Exp_PageAlter("用户密码重置操作成功,密码:abc");

                    }
                    else
                    {
                        this.Exp_PageAlter("用户密码重置操作失败");
                    }

                });

            });
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.EditSKey;
                Response.Redirect("MemberInfoManage.aspx?" + sKey);
            });
        }
        /// <summary>
        /// 用户验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                GetModelKey(model =>
                {
                    var RP = _Dal.UpdateIsApprove(model.id, !model.isApprove);
                    if(RP > 0)
                    {
                        txtYZ.Text = (txtYZ.Text == "已验证") ? "未验证" : "已验证";
                        this.Exp_PageWriteLog(Log_Type.other, "会员管理:", model.id, "验证状态");
                        this.Exp_PageAlter("用户验证操作成功,当前状态:" + txtYZ.Text);
                    }
                    else
                    {
                        this.Exp_PageAlter("用户验证操作失败");
                    }



                });
            });
        }
    }
}
